<?php
class TypesController extends StoreAppController {

	var $name = 'StoreTypes';
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'view' => array('admin','manag')
	);
	public function beforeFilter(){
		parent::beforeFilter();
		App::import('Model','Store.StoreCategory'); 
		$this->StoreCategory = new StoreCategory();
		App::import('Model','Store.StoreTypeGroup');
		$this->StoreTypeGroup = new StoreTypeGroup();		
//		$this->layout = 'cms';
	}
	
	function index() {
		$this->StoreType->recursive = 0;
		$this->paginate = array('limit' => 200);
		$this->set('storeTypes', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid store type', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('storeType', $this->StoreType->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->StoreType->create();
			if ($this->StoreType->save($this->data)) {
				$this->Session->setFlash(__d('store','The store type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store type could not be saved. Please, try again.', true));
			}
		}
		
		$parentCategory = $this->StoreCategory->find('threaded', array());
		$parentCategories = $this->treeForCombobox($parentCategory,'StoreCategory');
		$this->set('storeCategories',$parentCategories);
		
		$storeTypeGroup = $this->StoreTypeGroup->find('list');
		$this->set('storeTypeGroup',$storeTypeGroup);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__d('store','Invalid store type', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {			
			if ($this->StoreType->save($this->data)) {				
				$this->Session->setFlash(__d('store','The store type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store type could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$parentCategory = $this->StoreCategory->find('threaded', array());
			$parentCategories = $this->treeForCombobox($parentCategory,'StoreCategory');
			$this->set('storeCategories',$parentCategories);
			$storeTypeGroup = $this->StoreTypeGroup->find('list');
			$this->set('storeTypeGroup',$storeTypeGroup);						
			$this->data = $this->StoreType->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid id for store type', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->StoreType->delete($id)) {
			$this->Session->setFlash(__d('store','Store type deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__d('store','Store type was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>